package org.algorithm.落雪.数组.三月.第四周;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhx
 * @date 2022-03-21 星期一18:26
 *
 * url: https://leetcode-cn.com/problems/pascals-triangle/
 */
public class 杨辉三角I {

    public static void main(String[] args) {
        System.out.println(generate(5));
    }


    /**
     * 输入: numRows = 5
     * 输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
     *
     * 输入: numRows = 1
     * 输出: [[1]]
     */
    public static List<List<Integer>> generate(int numRows) {

        List<List<Integer>> result = new ArrayList<>();
        List<Integer> temp = null;//上一行数据
        for (int i = 1; i <= numRows; i++) {
            List<Integer> list = new ArrayList<>();
            if (i == 1) {
                list.add(1);
            }else {
                list.add(0,1);
                list.add(list.size()-1,1);
            }
            //通过上一行数据计算当前行中间值
            for (int j = 1; j < i - 1; j++) {
                list.add(j,temp.get(j-1) + temp.get(j));
            }

            temp = list;
            result.add(list);
        }

        return result;
    }

}
